package com.qqt.csr.im.assembler;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.google.common.collect.Lists;
import com.qqt.csr.im.entity.Account;
import com.qqt.csr.im.entity.Room;
import com.qqt.csr.im.entity.RoomMember;
import com.qqt.csr.im.entity.Conversation;
import com.qqt.csr.im.enums.ConversationStateEnum;
import com.qqt.csr.im.dto.BindResultDTO;
import com.qqt.csr.im.dto.ConversationDTO;
import org.apache.commons.lang3.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: yang hao
 * @Date: 2024/10/15 16:43
 * @Description: 会话装配器
 **/
public class ConversationAssembler {

    /**
     * 转换
     * @return {@link Conversation} 会话
     */
    public static Conversation convert(String initiatorId,String ext) {
        String date = DateUtil.now();
        return Conversation.builder()
                .state(ConversationStateEnum.NORMAL)
                .initiatorId(initiatorId)
                .startTime(date)
                .createTime(date)
                .updateTime(date)
                .ext(ext)
                .build();
    }

    /**
     * 转换
     * @param conversation {@link Conversation} 会话
     * @return {@link BindResultDTO} 绑定vo
     */
    public static BindResultDTO convert(Conversation conversation, Room room) {
        return BindResultDTO.builder()
                .conversation(conversation)
                // .initiator(initiator)
                // .recipient(recipient)
                .room(room)
                .build();
    }

    /**
     * 转换
     * @param conversations {@link Conversation} 会话
     * @param accountMap {@link List<Account>} 账户信息
     * @return {@link ConversationDTO} 会话信息
     */
    public static List<ConversationDTO> convert(List<Conversation> conversations, Map<Long, Account> accountMap, Map<Long, List<RoomMember>> roomMemMap) {
        if (conversations == null || conversations.isEmpty()) {
            return Collections.emptyList();
        }
        return conversations.stream()
                .map(conversation -> convert(conversation, accountMap, roomMemMap))
                .collect(Collectors.toList());
    }

    /**
     * 转换
     * @param conversation {@link Conversation} 会话
     * @param accountMap {@link List<Account>} 账户信息
     * @return {@link ConversationDTO} 会话信息
     */
    public static ConversationDTO convert(Conversation conversation, Map<Long, Account> accountMap, Map<Long, List<RoomMember>> roomMemMap) {
        ConversationDTO vo = BeanUtil.copyProperties(conversation, ConversationDTO.class);
        vo.setInitiator(StringUtils.isNotBlank(conversation.getInitiatorId()) ? accountMap.get(Long.parseLong(conversation.getInitiatorId())) : null);
        vo.setRoomId(Long.parseLong(conversation.getRoomId()));
        vo.setRoomMemberList(Lists.newArrayList());
        List<RoomMember> roomMemberList = Optional.ofNullable(roomMemMap.get(vo.getRoomId())).orElseGet(Lists::newArrayList);
        roomMemberList.forEach(roomMember -> {
            Account account = accountMap.get(roomMember.getAccountId());
            if (account != null) {
                vo.getRoomMemberList().add(account);
            }
        });
        return vo;
    }

}
